<?php
/**
 * 騰訊開發平臺
 * 
 * */
if(!defined('QQ_CLIENT_ID'))define('QQ_CLIENT_ID','100282708');
require_once(LIB_PATH.'spider/myspider.php');

class myopenqq {

	public static function isLogin(){
		if(myuser::isLogin()){
			if(myopenqq::getOpenId() == myuser::getNick()) return true;
		}
		
		return false;
	}
	
	public static function getOpenId(){
		return c('openId');
	}
	public static function getOpenName(){
		return c('openName');
	}

	public static function login($openId,$accessToken){
		setcookie('openId'   , $openId,(time()+7776000),'/','polang.org');
		setcookie('openToken', $accessToken,(time()+7776000),'/','polang.org');
		
		$userInfo = self::getUserInfo($accessToken,$openId);
		setcookie('openName' , $userInfo->nickname,(time()+7776000),'/','polang.org');
		setcookie('openPic'  , $userInfo->figureurl,(time()+7776000),'/','polang.org');

	}
	
	public static function getOpenPic(){
		return c('openPic');
	}

	public static function getAccessToken(){
		return c('openToken');
	}

	/**
	 * 獲取qq用戶信息
	 * http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91get_user_info
	 * */
	public static function getUserInfo($accessToken,$openid){
		/**
https://graph.qq.com/user/get_user_info? 
access_token=*************&
oauth_consumer_key=12345&
openid=****************&
format=json 
		 * */
		$url  = 'https://graph.qq.com/user/get_user_info?';
		$url .= 'access_token='.$accessToken.'&';
		$url .= 'oauth_consumer_key='.QQ_CLIENT_ID.'&';
		$url .= 'openid='.$openid.'&';
		$url .= 'format=json';
		$content = myspider::curl($url); //@todo 引入錯誤處理機制
		$userInfo = json_decode($content);
		return $userInfo;
	}


}
